<script lang="ts" setup>
import VConsole from 'vconsole'
import { useI18n } from 'vue-i18n'
import hotkeys from 'hotkeys-js'
import { ikMitt, ikStore } from 'iking-utils'
import useSettingsStore from '@g/store/modules/settings'
import useMenuStore from '@g/store/modules/menu'
import useTabbarStore from '@g/store/modules/tabbar'
import useTabbar from '@g/utils/composables/useTabbar'
import { getElementLocales } from '@/locales'

const route = useRoute()

const settingsStore = useSettingsStore()
const menuStore = useMenuStore()
const tabbarStore = useTabbarStore()
const tabbar = useTabbar()

const locales = computed(() => getElementLocales())

const buttonConfig = ref({
  autoInsertSpace: true
})

// 侧边栏主导航当前实际宽度
const mainSidebarActualWidth = computed(() => {
  let actualWidth = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--ik-main-sidebar-width'))
  if (settingsStore.settings.menu.menuMode === EMenuModel.SINGLE || ([EMenuModel.ONLY_HEAD, EMenuModel.HEAD].includes(settingsStore.settings.menu.menuMode) && settingsStore.mode !== 'mobile'))
    actualWidth = 0

  return `${actualWidth}px`
})

// 侧边栏次导航当前实际宽度
const subSidebarActualWidth = computed(() => {
  let actualWidth = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--ik-sub-sidebar-width'))
  if (settingsStore.settings.menu.subMenuCollapse && settingsStore.mode !== 'mobile')
    actualWidth = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--ik-sub-sidebar-collapse-width'))

  if ([EMenuModel.ONLY_SIDE, EMenuModel.ONLY_HEAD].includes(settingsStore.settings.menu.menuMode) && settingsStore.mode !== 'mobile')
    actualWidth = 0

  if (
    settingsStore.settings.menu.subMenuOnlyOneHide
      && menuStore.sidebarMenus.length === 1
      && (
        !menuStore.sidebarMenus[0].children
          || menuStore.sidebarMenus[0]?.children.every(item => item.meta?.sidebar === false)
      )
  )
    actualWidth = 0

  return `${actualWidth}px`
})

// 设置页面 title
watch([
  () => settingsStore.settings.app.enableDynamicTitle,
  () => settingsStore.title
], () => {
  if (settingsStore.settings.app.enableDynamicTitle && settingsStore.title) {
    const title = settingsStore.titleFirst
      ? settingsStore.title
      : settingsStore.settings.app.routeBaseOn !== 'filesystem'
        ? generateI18nTitle(route.meta.breadcrumbNeste?.at(-1)?.i18n, settingsStore.title)
        : generateI18nTitle(route.meta.i18n, settingsStore.title)
    document.title = `${title} - ${import.meta.env.VITE_APP_TITLE}`
  }
  else {
    document.title = import.meta.env.VITE_APP_TITLE
  }
  if (settingsStore.settings.tabbar.enable && settingsStore.titleFirst && settingsStore.title) {
    tabbarStore.editTitle({
      tabId: tabbar.getId(),
      title: settingsStore.title
    })
  }
}, {
  immediate: true
})

onMounted(() => {
  settingsStore.setMode(document.documentElement.clientWidth)
  window.onresize = () => {
    settingsStore.setMode(document.documentElement.clientWidth)
  }
  hotkeys('alt+i', () => {
    ikMitt.emit('global-system-info-toggle')
  })

  const { version } = __SYSTEM_INFO__
  const cache_version = ikStore.local.getItem('version')
  if (!cache_version || cache_version !== version) {
    localStorage.clear()
    ikStore.local.setItem('version', version)
  }
})

const { t, te } = useI18n()
provide(InjectionI18nTitle, generateI18nTitle)
function generateI18nTitle(key: string | undefined, defaultTitle: string | Function = t('route.undefined')) {
  return (settingsStore.settings.toolbar.enableI18n && !!key && te(key))
    ? t(key)
    : typeof defaultTitle === 'function'
      ? defaultTitle()
      : defaultTitle
}

import.meta.env.VITE_APP_DEBUG_TOOL === 'vconsole' && new VConsole()
</script>

<template>
  <el-config-provider :locale="locales[settingsStore.settings.app.defaultLang]" :size="settingsStore.settings.app.elementSize" :button="buttonConfig">
    <RouterView
      :style="{
        '--ik-main-sidebar-actual-width': mainSidebarActualWidth,
        '--ik-sub-sidebar-actual-width': subSidebarActualWidth,
      }"
    />
    <IkSystemInfo />
  </el-config-provider>
</template>
